<template>
  <view class="main">
    <view class="textBox">
      <u-textarea maxlength="300" border="none" v-model="formData.content" placeholder="请详细描述你的问题" :count="true"></u-textarea>
    </view>
    <view class="scBox">
      <image v-if="formData.imgUrl" class="box" :src="formData.imgUrl" mode=""></image>
      <view v-else class="box" @click="uploadImg">
        <u-icon name="plus" color="#C9CDD8" size="30"></u-icon>
      </view>
    </view>
    <view class="inputBox">
      <view class="title">联系方式</view>
      <up-input placeholder="请输入手机号或者邮箱" border="none" v-model="formData.cell"></up-input>
    </view>
    <view class="bottomBox">
      <u-button @click="submit" shape="circle" text="提交" color="#71cec5"></u-button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { BASE_UPLOADURL } from "@/utils/request";
import { feedBack } from "@/api/my";
import params from "@/utils/publicParams";
const formData = ref({
  cell: "",
  content: "",
  imgUrl: "",
  userId: uni.getStorageSync("userId"),
});
const uploadImg = () => {
  uni.chooseImage({
    count: 1, //默认9
    sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
    sourceType: ["album", "camera"], //从相册选择或者打开相机
    success: function (res) {
      let tempFilePaths = res.tempFilePaths;
      for (let i = 0, len = tempFilePaths.length; i < len; i++) {
        uni.showLoading({
          title: "加载中",
        });
        uni.uploadFile({
          url: BASE_UPLOADURL,
          filePath: res.tempFilePaths[0],
          name: "fileImage",
          formData: {
            ...params,
            userId: uni.getStorageSync("userId"),
          },
          success: async (uploadFileRes) => {
            console.log(uploadFileRes);
            formData.value.imgUrl = JSON.parse(uploadFileRes.data).imgUrl;
            uni.showToast({
              title: "修改成功",
            });
          },
          complete() {
            //隐藏loading
            uni.hideLoading();
          },
        });
      }
    },
  });
};

const submit = async () => {
  if (!formData.value.content) {
    uni.showToast({ icon: "none", title: "请填写内容" });
    return;
  } else if (!formData.value.cell) {
    uni.showToast({ icon: "none", title: "请填写联系方式" });
    return;
  }
  await feedBack(formData.value);
  uni.showToast({
    title: "提交成功",
  });
  setTimeout(() => {
    uni.switchTab({
      url: "/pages/home/home",
    });
  }, 1000);
};
</script>

<style lang="less" scoped>
.textBox {
  margin-top: 20rpx;
}
.scBox {
  display: flex;
  background: #fff;
  margin-top: 20rpx;
  padding: 32rpx;
  .box {
    width: 160rpx;
    height: 160rpx;
    border-radius: 8rpx;
    background: #f5f8fc;
    margin-right: 5rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
  }
}
.inputBox {
  background: #ffffff;
  margin-top: 20rpx;
  padding: 32rpx;
  .title {
    font-size: 28rpx;
    color: #1e1e1e;
    line-height: 48rpx;
    margin-bottom: 24rpx;
  }
}
</style>
